import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue'
import Test from '../views/Test.vue'
import Course from '../views/Course.vue'
import Html from '../views/Html.vue'
import Css from '../views/Css.vue'
import Login from '../views/Login.vue'
Vue.use(VueRouter)

const routes = [{
		path: '/home',
		name: 'home',
		component: HomeView
	},
	{
		path: '/about',
		name: 'about',
		component: AboutView,
		//路由独享守卫
		beforeEnter: function(to, from, next) {
            // document.title = '哈哈';
			console.log("路由独享守卫")
			next();
		}
	},
	{
		path: '/course',
		component: Course,
		// redirect: '/css', //路径重定向
		redirect: {
			name: 'html' //命名重定向
		},
		//二级路由
		children: [{
			path: '/html',
			name: 'html',
			component: Html,
			meta: {
				title: 'html'
			}
		}, {
			path: '/css',
			name: 'css',
			component: Css,
			meta: {
				title: 'css'
			}
		}, {
			// path: '/test/:name/:id',
			path: '/test',
			name: 'Test',
			component: Test,
			props: true,
			meta: {
				title: 'test'
			}
		}]
	},
	{
		path: '/login',
		name: 'Login',
		component: Login,
	},
	{
		path: '/',
		redirect: {
			name: 'Login' //命名重定向
		}
	}
]

const router = new VueRouter({
	routes,
	mode: 'history'
})

//全局前置守卫
router.beforeEach(function(to, from, next) {
	// console.log(to)
	// console.log(from)
	// console.log("---------------------------")
	let apiKey = localStorage.getItem("apiKey");

	/* if (to.name == 'Login' || apiKey == '123') {
		next();//放行
	} else {
		next({
			name: 'Login'//放行
		})
	}*/

	//鉴权
	if (to.name != 'Login' && apiKey != '123') {
		next({
			name: 'Login' //放行
		}) //放行
	} else {
		next();
	}

})

//全局后置守卫
router.afterEach(function(to, from) {
	console.log(to)
	// console.log(from)
	document.title = to.meta.title
})
export default router
